﻿var mins
var secs;
var canSubmit = 1;

function cd(countDown) {
    mins = 1 * m(countDown); // change minutes here
    secs = 0 + s(":01"); // change seconds here (always add an additional second to your total)
    redo();
}

function m(obj) {
    for (var i = 0; i < obj.length; i++) {
        if (obj.substring(i, i + 1) == ":")
            break;
    }
    return (obj.substring(0, i));
}

function s(obj) {
    for (var i = 0; i < obj.length; i++) {
        if (obj.substring(i, i + 1) == ":")
            break;
    }
    return (obj.substring(i + 1, obj.length));
}

function dis(mins, secs) {
    var disp;
    if (mins <= 9) {
        disp = " 0";
    } else {
        disp = " ";
    }
    disp += mins + ":";
    if (secs <= 9) {
        disp += "0" + secs;
    } else {
        disp += secs;
    }
    return (disp);
}

function redo() {
    secs--;
    if (secs == -1) {
        secs = 59;
        mins--;
    }
    document.getElementById("spTimer").innerHTML = dis(mins, secs); // setup additional displays here.
    if ((mins == 0) && (secs == 0)) {
        if (canSubmit == 1) {
            ClearTheTimer();
            if (document.getElementById("submitFlag").value == "1") {
                document.forms[0].submit();
                DisplayLoadingBar();
            }
        }
    } else {
        cd = setTimeout("redo()", 1000);
    }
}

//start to count down
function startTiming(counter) {
    cd(counter.toString());
}


//paging code
var currentPageIndex = 1;
var pageArray = new Array();
pageArray = qdivstring.split(',');


function startPaging(offset) {
    if (canNext == 0) {
        if (parseInt(offset) >= 0) {
            return;
        }
    }
    if (canPrevious == 0) {
        if (parseInt(offset) < 0) {
            return;
        }
    }
    if (parseInt(offset) >= 0) {
        if (canLeaveoutQuestion.toString() == "False") {
            var bl = checkCanLeaveoutQuestion();
            if (bl == false) {
                return;
            }
        }
    }

    EnablePageButtons();

    if (offset.toString() == "-2") {
        currentPageIndex = 1;
    }
    else if (offset.toString() == 0) {
        currentPageIndex = pageArray.length;
    }
    else {
        currentPageIndex = parseInt(currentPageIndex) + parseInt(offset);
    }

    if (parseInt(currentPageIndex) <= 0) {

        currentPageIndex = 1;
    }
    if (parseInt(currentPageIndex) >= pageArray.length) {

        currentPageIndex = pageArray.length;
    }
    if (currentPageIndex == 1) {
        DisableThePreviousPageButton();
    }
    if (currentPageIndex == pageArray.length) {
        DisableTheNextPageButton();
    }
    else {
        document.getElementById("bntSubmit").style.display = "none";
    }
    paging(currentPageIndex);
}

var canNext = 1;
var canPrevious = 1;
function DisableTheNextPageButton() {
    document.getElementById("imgNextCenter").style.backgroundImage = "url(../include/exam_image/btn/btn09.gif)";
    document.getElementById("imgNextRight").src = "../include/exam_image/btn/btn08.gif";
    document.getElementById("imgNextLeft").src = "../include/exam_image/btn/btn07.gif";
    document.getElementById("bntSubmit").style.display = "block";

    document.getElementById("imgLastCenter").style.backgroundImage = "url(../include/exam_image/btn/btn09.gif)";
    document.getElementById("imgLastRight").src = "../include/exam_image/btn/btn08.gif";
    document.getElementById("imgLastLeft").src = "../include/exam_image/btn/btn07.gif";
    canNext = 0;
}

function DisableThePreviousPageButton() {
    document.getElementById("imgPreviousCenter").style.backgroundImage = "url(../include/exam_image/btn/btn09.gif)";
    document.getElementById("imgPreviousRight").src = "../include/exam_image/btn/btn08.gif";
    document.getElementById("imgPreviousLeft").src = "../include/exam_image/btn/btn07.gif";

    document.getElementById("imgFirstCenter").style.backgroundImage = "url(../include/exam_image/btn/btn09.gif)";
    document.getElementById("imgFirstRight").src = "../include/exam_image/btn/btn08.gif";
    document.getElementById("imgFirstLeft").src = "../include/exam_image/btn/btn07.gif";
    canPrevious = 0;
}

function EnablePageButtons() {
    document.getElementById("imgPreviousCenter").style.backgroundImage = "url(../include/exam_image/btn/btn06.gif)";
    document.getElementById("imgPreviousRight").src = "../include/exam_image/btn/btn05.gif";
    document.getElementById("imgPreviousLeft").src = "../include/exam_image/btn/btn04.gif";

    document.getElementById("imgFirstCenter").style.backgroundImage = "url(../include/exam_image/btn/btn06.gif)";
    document.getElementById("imgFirstRight").src = "../include/exam_image/btn/btn05.gif";
    document.getElementById("imgFirstLeft").src = "../include/exam_image/btn/btn04.gif";

    document.getElementById("imgNextCenter").style.backgroundImage = "url(../include/exam_image/btn/btn06.gif)";
    document.getElementById("imgNextRight").src = "../include/exam_image/btn/btn05.gif";
    document.getElementById("imgNextLeft").src = "../include/exam_image/btn/btn04.gif";
    document.getElementById("bntSubmit").style.display = "block";

    document.getElementById("imgLastCenter").style.backgroundImage = "url(../include/exam_image/btn/btn06.gif)";
    document.getElementById("imgLastRight").src = "../include/exam_image/btn/btn05.gif";
    document.getElementById("imgLastLeft").src = "../include/exam_image/btn/btn04.gif";
    canNext = 1;
    canPrevious = 1;
}

function checkCanLeaveoutQuestion() {

    var obj = document.getElementById(pageArray[parseInt(currentPageIndex) - 1]);
    var inputElements = obj.getElementsByTagName('input');
    var namesArray = new Array();

    ////place all the elements' name into this array
    for (var i = 0; i < inputElements.length; i++) {
        if (!arrayContains(namesArray, inputElements[i].name)) {
            namesArray[namesArray.length] = inputElements[i].name;
        }
    }

    for (var i = 0; i < namesArray.length; i++) {
        var subElements = document.getElementsByName(namesArray[i]);
        if (subElements[0].type == 'radio' || subElements[0].type == 'checkbox') {
            var leaveOut = true;
            for (var j = 0; j < subElements.length; j++) {
                if (subElements[j].checked) {
                    leaveOut = false;
                }
            }
            if (leaveOut == true) {
                alert(leaveOutMsg);
                subElements[0].focus();
                return false;
            }
        }
    }

    var textereaElements = obj.getElementsByTagName('textarea');
    for (var i = 0; i < textereaElements.length; i++) {

        if (textereaElements[i].value == "") {
            alert(leaveOutMsg);
            textereaElements[i].focus();
            return false;
        }
    }

    return true;
}

////true: the array contains the target
////false: the array does not contains the target
function arrayContains(str, target) {
    for (var i = 0; i < str.length; i++) {
        if (str[i] == target) {
            return true;
        }
        return false;
    }
}

function initialPage() {
    if (pageArray.length == 1) {
        document.getElementById("bntSubmit").style.display = "block";
        DisableTheNextPageButton();
    }
    else {
        document.getElementById("bntSubmit").style.display = "none";
    }

    DisableThePreviousPageButton();
}

function paging(pageIndex) {

    for (var i = 0; i < pageArray.length; i++) {
        if (i != (parseInt(pageIndex) - 1)) {
            document.getElementById(pageArray[i]).style.display = "none";
        }
        else {
            document.getElementById(pageArray[i]).style.display = "block";
        }
    }
}

function document.onkeydown() {
    if (event.keyCode == 116) {
        event.keyCode = 0;
        event.cancelBubble = true;
        return false;
    }
}

///execute this function after the examination
var targetUrl;
function EndAExamPaper(mtestResult1, murl1) {
    if (mtestResult1 == "") {
        return;
    }

    targetUrl = murl1;
    document.getElementById('blSubmitResult').innerHTML = mtestResult1;
    tipsWindown("PASS", "id:examResultHintDiv", "890", "400", "true", "", "true", "floatDiv");
}

function ClientReact(isClose) {
    window.opener.document.getElementById("btnRebindData").click();
    if (targetUrl != "") {
        if (isClose == "1") {
            window.location = targetUrl;
        }
        else {
            window.close();
        }
    }
    else {
        window.close();
    }
}

function ClearTheTimer() {
    canSubmit = 0;
}

function SubmitForm() {
    if (canLeaveoutQuestion.toString() == "False") {
        var bl = checkCanLeaveoutQuestion();
        if (bl == false) {
            return;
        }
    }
    if (confirm(ConfirmSubmit)) {
        document.getElementById("divTimerRoom").style.display = "none";
        ClearTheTimer();
        document.getElementById("trPagingPanel").style.display = "none";
        if (document.getElementById("submitFlag").value == "1") {
            document.forms[0].submit();
        }
        DisplayLoadingBar();
    }
}

function DisplayLoadingBar() {
    tipsWindown("PASS", "id:divWaitingMark", "250", "250", "true", "", "true", "divWaitingMark");
    initFloatTips("divWaitingMark");
}

//<![CDATA[
var tips; var theTop = 250/*height by default*/; var old = theTop;
function initFloatTips(floatTips) {
    tips = document.getElementById(floatTips);
    moveTips();
};
function moveTips() {
    var tt = 50;
    if (window.innerHeight) {
        pos = window.pageYOffset
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        pos = document.documentElement.scrollTop
    }
    else if (document.body) {
        pos = document.body.scrollTop;
    }
    pos = pos - tips.offsetTop + theTop;
    pos = tips.offsetTop + pos / 10;
    if (pos < theTop) pos = theTop;
    if (pos != old) {
        tips.style.top = pos + "px";
        tt = 10;
    }
    old = pos;
    setTimeout(moveTips, tt);
}
//!]]>